package cn.iocoder.yudao.module.business.controller.admin.trainUnit;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.idempotent.core.annotation.Idempotent;
import cn.iocoder.yudao.module.business.controller.admin.trainUnit.vo.TrainUnitPageReqVO;
import cn.iocoder.yudao.module.business.controller.admin.trainUnit.vo.TrainUnitRespVO;
import cn.iocoder.yudao.module.business.controller.admin.trainUnit.vo.TrainUnitSaveReqVO;
import cn.iocoder.yudao.module.business.dal.dataobject.trainUnit.TrainUnitDO;
import cn.iocoder.yudao.module.business.service.trainUnit.TrainUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 培训单位")
@RestController
@RequestMapping("/zljc/train-unit")
@Validated
public class TrainUnitController {

    @Resource
    private TrainUnitService trainUnitService;

    @PostMapping("/create")
    @Operation(summary = "创建培训单位")
    @PreAuthorize("@ss.hasPermission('zljc:train-unit:create')")
    @Idempotent(timeout = 5,message = "重复请求，请稍后再试")
    public CommonResult<Long> createTrainUnit(@Valid @RequestBody TrainUnitSaveReqVO createReqVO) {
        return success(trainUnitService.createTrainUnit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新培训单位")
    @PreAuthorize("@ss.hasPermission('zljc:train-unit:update')")
    public CommonResult<Boolean> updateTrainUnit(@Valid @RequestBody TrainUnitSaveReqVO updateReqVO) {
        trainUnitService.updateTrainUnit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除培训单位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zljc:train-unit:delete')")
    public CommonResult<Boolean> deleteTrainUnit(@RequestParam("id") Long id) {
        trainUnitService.deleteTrainUnit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得培训单位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zljc:train-unit:query')")
    public CommonResult<TrainUnitRespVO> getTrainUnit(@RequestParam("id") Long id) {
        TrainUnitDO trainUnit = trainUnitService.getTrainUnit(id);
        return success(BeanUtils.toBean(trainUnit, TrainUnitRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得培训单位分页")
    @PreAuthorize("@ss.hasPermission('zljc:train-unit:query')")
    public CommonResult<PageResult<TrainUnitRespVO>> getTrainUnitPage(@Valid TrainUnitPageReqVO pageReqVO) {
        PageResult<TrainUnitDO> pageResult = trainUnitService.getTrainUnitPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TrainUnitRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得培训单位列表")
    @PreAuthorize("@ss.hasPermission('zljc:train-unit:query')")
    public CommonResult<List<TrainUnitRespVO>> getTrainUnitList(@Valid TrainUnitPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TrainUnitDO> list = trainUnitService.getTrainUnitPage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, TrainUnitRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出培训单位 Excel")
    @PreAuthorize("@ss.hasPermission('zljc:train-unit:export')")
    public void exportTrainUnitExcel(@Valid TrainUnitPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TrainUnitDO> list = trainUnitService.getTrainUnitPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "培训单位.xls", "数据", TrainUnitRespVO.class,
                        BeanUtils.toBean(list, TrainUnitRespVO.class));
    }

    @GetMapping("/syncTrainUnit")
    @Operation(summary = "同步培训单位")
    @PreAuthorize("@ss.hasPermission('zljc:train-unit:sync')")
    public CommonResult<Integer> getSyncTrainUnitList(TrainUnitPageReqVO pageReqVO) {
        return success(trainUnitService.executeSyncTrainUnit(pageReqVO));
    }
}